﻿; $(function () {
    
    $("#toolbar").ligerToolBar({
        items: [{
            text: '添加',
            icon: '',
            id: 'btnPartitionSave',
            click: function (parameters) {
                var rows = manager.getSelectedRows();
                if (rows && rows.length == 1) {
                    var row = manager.getSelectedRow();
                    if (row && row.PartitionId != "") {
                        location.href = "/Product/OpPartition?op=add&pId=" + row.PartitionId;
                    } else {
                        $.ligerDialog.warn('未选择类别');
                    }
                } else {
                    $.ligerDialog.warn('未选择类别');
                }
            }
        },
            {
                text: '编辑',
                icon: '',
                id: 'btnPartitionEdit',
                click: function (parameters) {
                    var rows = manager.getSelectedRows();
                    if (rows && rows.length == 1) {
                        var row = manager.getSelectedRow();
                        if (row && row.PartitionId != "") {
                            location.href = "/Product/OpPartition?op=edit&pId=" + row.PartitionId;
                        } else {
                            $.ligerDialog.warn('未选择类别');
                        }
                    } else {
                        $.ligerDialog.warn('未选择类别');
                    }
                }
            },
            {
            text: '删除',
            icon: '',
            id: 'btnPartitionDelete',
            click: function (parameters) {

                var rows = manager.getSelectedRows();
                if (rows && rows.length > 0) {
                    var ids = new Array();
                    $.each(rows, function () {
                        if (this.Level > 0) {
                            ids.push(this.PartitionId);
                        }
                    });
                    $.ajax({
                        url: '/Product/Delete',
                        dataType: 'json',
                        type:'post',
                        data: { ids: ids.join(",")},
                        traditional: true,
                        success: function (callback) {
                            if (callback) {
                                $.messageTip().show("删除成功！", 2000, function () {
                                    manager.loadData();
                                });
                            }
                            else {
                                $.messageTip().show("删除失败！");
                            }
                        },
                        error: function (ex) {
                            $.messageTip().show("删除失败！");
                        }
                    });
                } else {

                }

                
            }
        }]
    });

    var manager = $("#partitionGrid").ligerGrid({
        columns: [
            //{
            //    display: '序号',
            //    align: 'center',
            //    width: 50,
            //    minWidth: 50,
            //    render: function (item, i) {
            //        return (i + 1).toString();
            //    }
            //},
            {
                display: '类别名称',
                align: 'left',
                width: 200,
                minWidth: 100,
                //name: 'Name'
                //,
                render: function (item, i, value) {
                    var $wrap = $("<div />"),
                        $span = $("<span />"),
                        $text = $("<span />");

                    if (item.HasChildren == 1) {
                        $span.addClass("ifunny-grid-tree-link l-grid-tree-link-open")
                            .height(12)
                            .width(12);
                    }
                    $span.attr("data-item", JSON.stringify(item))
                        .css("margin-left", (item.Level * 10))
                        .html("&nbsp;&nbsp;&nbsp;");
                    $text.css("padding", "0 2px").text(item.Name);
                    if (item.Level == 0) {
                        $text.attr("title", item.PartitionId);
                    }

                    $wrap.append($span).append($text);
                    return $wrap.html();
                }
            },
            { display: '类别描述', name: 'Description', isSort: false, width: 250, align: 'left' },
            {
                display: '是否启用', name: 'Enabled', isSort: false, width: 80, align: 'center', render: function (item, i, value) {
                    return item.Enabled == 1 ? "启用" : "禁用";
                }
            },
            {
                display: '主页显示', name: 'Enabled', isSort: false, width: 80, align: 'center', render: function (item, i, value) {
                    return item.ShowInt == 1 ? "是" : "否";
                }
            }
        ,
            {
                display: '排序号', name: 'SortNumber', isSort: false, width: 80, align: 'center'
            }
            //,
            //{
            //    isSort: false,
            //    headerRender: function (item, i, value) {
            //        //var html = "<div style='margin-top:-2px;'><input class='partition-checkbox' type='checkbox' ";
            //        //html += " data-partitionid = '" + item.PartitionId + "'";
            //        //html += "></div>";
            //        //return html;
            //    },
            //    name: 'CheckBox',
            //    align: 'center',
            //    width: 60,
            //    minWidth: 60,
            //    render: function (item, i, value) {
            //        var $wrapper = $("<div />").css("margin-top", -1);
            //        var $checkbox = $("<input type='checkbox' />")
            //            .addClass("partition-checkbox")
            //            .data("partitionid", item.PartitionId);

            //        if (item.Level == 0) {
            //            $checkbox.attr("disabled", "disabled");
            //        }
            //        return $("<div />").append($wrapper.append($checkbox)).html();
            //    }
            //}
        ],
        width: '99.85%',
        height:
            '100%',
        heightDiff:
            -20,
        pageSizeOptions:
            [5, 10, 15, 20],
        usePager:false,
        //data: TreeDeptData,
        alternatingRow:
            false,
        url:
            "/Product/GridData",
        headerRowHeight:
            26,
        tree:
        {
            columnId: 'Name'
        },
        checkbox: true,
        autoCheckChildren:false,
        dataAction: "server",
        rowAttrRender:
            function (item, index) {
                return "partitionid='" + item.PartitionId
                    + "' parentid='" + item.ParentId
                    + "' level='" + item.Level + "'";
            },

        onAfterShowData: function () {

            $(".l-grid-row").each(function(i,v) {
                if ($(this).attr("level") == 0) {
                    $(this).css("font-weight","bold");
                }
            });
            //#region 所有的复选框 并加上事件

            //var $checkbox = $("#partitionGrid td.l-grid-row-cell .l-grid-row-cell-btn-checkbox");

            //$checkbox.each(function () {
            //    var data = $(this).parents(".l-grid-row").attr("level");
            //    if (data == 0) {
            //        $(this).ligerGetCheckBoxManager().setDisabled();
            //    }
                //if (data && data.Level == 0) {
                //    alert(1);
                //$(this).ligerGetCheckBoxManager().setDisabled();
                //}
                //var checked = this.checked;
                //var pid = $(this).data("partitionid");
                //$("#partitionGrid td.l-grid-row-cell input[data-partitionid^=" + pid + "]").each(function () {
                //    this.checked = checked;
                //    if (this.checked) {
                //        $(this).prev(".l-checkbox").addClass("l-checkbox-checked");
                //    } else {
                //        $(this).prev(".l-checkbox").removeClass("l-checkbox-checked");
                //    }
                //});
                //$(this).ligerCheckBox({ disabled: $(this).attr("disabled") == "disabled" });
            //});

            //#endregion


            //#region 遍历所有 '展开/关闭' 图标 加上事件
            $("#partitionGrid .ifunny-grid-tree-link").click(function (e) {
                //如果提供了事件对象，则这是一个非IE浏览器
                if (e && e.stopPropagation)
                    //因此它支持W3C的stopPropagation()方法
                {
                    e.stopPropagation();
                } else
                    //否则，我们需要使用IE的方式来取消事件冒泡
                {
                    window.event.cancelBubble = true;
                }
                var item = $(this).data("item");
                if ($(this).hasClass("l-grid-tree-link-open")) {
                    $(this).removeClass("l-grid-tree-link-open").addClass("l-grid-tree-link-close");
                    $("#partitionGrid tr.l-grid-row[parentid^=" + item.PartitionId + "]").hide();

                } else {
                    $(this).removeClass("l-grid-tree-link-close").addClass("l-grid-tree-link-open");
                    showChildren(item.PartitionId, item.Level + 1);
                }
            });
            //#endregion

        }
    }
    );
});

//显示子节点

function showChildren(pid, i) {
    var $children = $("#partitionGrid tr.l-grid-row[parentid^=" + pid + "]").filter(function (index) {
        return $(this).attr("level") == i;
    });
    if ($children.lenth == 0) {
        return false;
    }
    $.each($children, function (i, v) {
        var $treeTrigger = $(this).find(".ifunny-grid-tree-link");
        if ($treeTrigger.length > 0) {
            if ($treeTrigger.hasClass("l-grid-tree-link-open")) {
                var item = $treeTrigger.data("item");
                showChildren(item.PartitionId, item.Level + 1);
            }
        }
        $(this).show();
    });
}